#include "frmupdate.h"
#include "ui_frmupdate.h"

FrmUpdate::FrmUpdate(int place, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::FrmUpdate)
{
    ui->setupUi(this);
    Employee *emp = EmployeeContainer::employeelist->at(place);
    ui->letId->setText(QString::number(emp->id));
    ui->letName->setText(QString::fromStdString(emp->name));
    ui->letPhone->setText(QString::fromStdString(emp->phone));
    ui->letDepart->setText(QString::fromStdString(emp->depart));
    uplace = place;
}

FrmUpdate::~FrmUpdate()
{
    delete ui;
}

bool FrmUpdate::judge(int id, string name, string phone, string depart){
    if(id <=0 || name == "" || phone == "" || depart == ""){
        QMessageBox::critical(this, "错误", "添加失败！可能原因:\n编号小于1\n姓名、电话或部门为空！");
        return false;
    }
    return true;
}

void FrmUpdate::on_btnChange_clicked()
{
    int id = ui->letId->text().toInt();
    string name = ui->letName->text().toStdString();
    string phone = ui->letPhone->text().toStdString();
    string depart = ui->letDepart->text().toStdString();
    //修改员工
    if(judge(id, name, phone, depart)){
        EmployeeContainer::employeelist->at(uplace)->id = id;
        EmployeeContainer::employeelist->at(uplace)->name = name;
        EmployeeContainer::employeelist->at(uplace)->phone = phone;
        EmployeeContainer::employeelist->at(uplace)->depart = depart;
        this->close();
    }
}
